<template >
  <div class="app-container">

    <el-input
      v-model="filterText"
      placeholder="输入关键字进行过滤"/>

    <el-tree
      ref="tree"
      :data="subjectList"
      :props="defaultProps"
      :filter-node-method="filterNode" />
  </div>
</template>
<script>
import subjectApi from '@/api/subject'

export default {

  data() {
    return {
      filterText: '', // 过滤条件
      subjectList: [], // 数据列表
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },

  watch: {
    // filterText(newValue, oldValue) {
    //   console.log(newValue, oldValue)
    // }
    // 过滤功能
    filterText(val) {
      console.log(val)
      this.$refs.tree.filter(val)
    }
  },

  created() {
    this.fetchNodeList()
  },

  methods: {
    fetchNodeList() {
      subjectApi.nestedList().then(response => {
        this.subjectList = response.data.items
      })
    },

    filterNode(value, data) {
      console.log('----------------------')
      console.log(value)
      console.log(data)
      if (!value) return true
      // toLowerCase 忽略大小写
      return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
    }
  }
}
</script>
